package com.ruoyi.team.service.impl;

import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.team.mapper.CustomerTeamMapper;
import com.ruoyi.team.domain.CustomerTeam;
import com.ruoyi.team.service.ICustomerTeamService;

/**
 * 客户信息Service业务层处理
 *
 * @author AC
 * @date 2022-11-27
 */
@Service
public class CustomerTeamServiceImpl extends ServiceImpl<CustomerTeamMapper, CustomerTeam> implements ICustomerTeamService {

    @Autowired
    private ISysUserService userService;


    /**
     * 查询客户信息列表
     *
     * @param customerTeam 客户信息
     * @return 客户信息
     */
    @Override
    public List<CustomerTeam> selectCustomerTeamList(CustomerTeam customerTeam) {
        List<CustomerTeam> customerTeams = getBaseMapper().selectCustomerTeamList(customerTeam);
        for (CustomerTeam team : customerTeams) {
            team.setInputNickName(userService.selectUserById(team.getInputUser()).getNickName());
            team.setLinkName(team.getInputNickName());
        }
        return customerTeams;
    }


    //公用方法:判断重复
    private  boolean repetition(CustomerTeam entity){
        //第一步:根据企业名字查询
        CustomerTeam customerTeam = getBaseMapper().selectByCustomerName(entity.getCustomerName());
        //如果 != null 重复 抛异常
if (customerTeam != null){
    throw new RuntimeException("企业名不能重复");
}else {
    //如果 = 则添加
    return true;
} }


    @Override
    public boolean save(CustomerTeam entity) {
         this.repetition(entity);
            // 获取登录对象 设置为录入人
            // LoginUser loginUser = SecurityUtils.getLoginUser();
            entity.setInputUser(SecurityUtils.getLoginUser().getUserId());
            // 获取当前时间 设置为录入时间
            entity.setInputTime(new Date());
            int insert = getBaseMapper().insert(entity);
            return insert > 0;
    }

    @Override
    public boolean updateById(CustomerTeam entity) {
//        this.repetition(entity);//修改时判断公司名不重复
        int i = getBaseMapper().updateById(entity);
        return i >0;
    }
}
